package com.example.media.slice;

import com.example.media.ResourceTable;
import com.example.media.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.SurfaceOps;
import ohos.global.resource.RawFileDescriptor;
import ohos.media.common.Source;
import ohos.media.player.Player;

public class PlayerVideoAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    // SurfaceProvider对象
    private SurfaceProvider mSurfaceProvider;
    private Button mBtnVideoPlay;
    // 播放器对象
    private Player mPlayer;

    private Button mBtnVideoPause;
    private Button mBtnVideoStop;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_player_video);
        mSurfaceProvider = (SurfaceProvider) findComponentById(ResourceTable.Id_surface_provider_video);
        mBtnVideoPlay = (Button) findComponentById(ResourceTable.Id_btn_video_play);
        mBtnVideoPlay.setClickedListener(this);
        mBtnVideoPause = (Button) findComponentById(ResourceTable.Id_btn_video_pause);
        mBtnVideoPause.setClickedListener(this);
        mBtnVideoStop = (Button) findComponentById(ResourceTable.Id_btn_video_stop);
        mBtnVideoStop.setClickedListener(this);

        mSurfaceProvider.pinToZTop(true);
        mSurfaceProvider.getSurfaceOps().get().addCallback(new SurfaceOps.Callback() {
            @Override
            public void surfaceCreated(SurfaceOps surfaceOps) {
                // 初始化播放器
                initPlayer();
            }

            @Override
            public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceOps surfaceOps) {
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {

        // 播放视频
        if (component.getId() == ResourceTable.Id_btn_video_play) {
            mPlayer.play();
        }
        // 暂停视频
        if (component.getId() == ResourceTable.Id_btn_video_pause) {
            mPlayer.pause();
        }
        // 停止视频
        if (component.getId() == ResourceTable.Id_btn_video_stop) {
            mPlayer.stop();
            mPlayer.release();
            initPlayer();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPlayer.stop();
        mPlayer.release();
    }

    /**
     * 初始化Player对象
     */
    private void initPlayer() {
        // 创建播放器对象
        mPlayer = new Player(this);
        try {
            // 打开播放音频源文件
            RawFileDescriptor fd = getResourceManager()
                    .getRawFileEntry("resources/rawfile/test.mp4")
                    .openRawFileDescriptor();
            Source source = new Source(fd.getFileDescriptor(),
                    fd.getStartPosition(),
                    fd.getFileSize());
            // 设置音频源
            mPlayer.setSource(source);

        } catch (Exception e) {
            Utils.log("Exception : " + e.getLocalizedMessage());
        }
        // 准备播放
        mPlayer.prepare();
        // 设置Surface配置
        mPlayer.setSurfaceOps(mSurfaceProvider.getSurfaceOps().get());
    }
}
